package fabriques;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import donnees.Client;

public class FabClient {
	public static FabClient fc;
	
	private static PreparedStatement pcreer;
	private static String creer = "insert into Client values(?,?,?,?,?,?)";
	private static PreparedStatement pIDMax;
	private static String IDMax = "select max(idClient) from Client";
	private static PreparedStatement prechercherLeClientDeLaCommande;
	private static String rechercherLeClientDeLaCommande= "select idClient from Client,Commande where Client.idClient = Commande.idClient and idCommande=?";
	private static PreparedStatement prechercher;
	private static String rechercher = "select * from Client where idClient=?";
	private static PreparedStatement prechercherViaMail;
	private static String rechercherViaMail = "select * from Client where mail=?";
	private static PreparedStatement plister;
	private static String lister = "select * from Client";

	public static synchronized FabClient getInstance(){
		if(fc == null)
			fc = new FabClient();
		return fc;
	}
	
	public static void setConnection(Connection c){
		try {
			pcreer = c.prepareStatement(creer);
			pIDMax = c.prepareStatement(IDMax);
			prechercherLeClientDeLaCommande = c.prepareStatement(rechercherLeClientDeLaCommande);
			prechercher = c.prepareStatement(rechercher);
			prechercherViaMail = c.prepareStatement(rechercherViaMail);
			plister = c.prepareStatement(lister);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private FabClient(){
		Connexion.getInstance();
		Connection con = Connexion.getCon();
		setConnection(con);
	}

	public Client creer(String nom,String prenom,String mail,String motdepasse,int idAdr){
		int idPM = 0;
		/* on r�cup�re le dernier id afin de cr�er un nouvel ID coh�rent pour la base. */
		ResultSet res = null;
		try {
			res = pIDMax.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(res.next())
					idPM = res.getInt("max(idClient)");
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		idPM++;
		
		try {
			pcreer.setInt(1,idPM);
			pcreer.setString(2,nom);
			pcreer.setString(3,prenom);
			pcreer.setString(4,mail);
			pcreer.setString(5,motdepasse);
			pcreer.setInt(6, idAdr);
			pcreer.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return new Client(idPM,nom,prenom,mail,motdepasse);
		
	}
	
	public Client rechercher(int id) throws SQLException{
		ResultSet res = null;
		try {
			prechercher.setInt(1, id);
			res = prechercher.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		res.next();
		return new Client(res.getInt(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
	}
	
	public Client rechercherViaMail(String mail) throws SQLException{
		ResultSet res = null;
		try {
			prechercherViaMail.setString(1, mail);
			res = prechercherViaMail.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		res.next();
		return new Client(res.getInt(1),res.getString(2),res.getString(3),res.getString(4),res.getString(5));
	}
	
	public boolean mailIsInBDD(String mail){
		ResultSet res = null;
		try {
			prechercherViaMail.setString(1, mail);
			res = prechercherViaMail.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if(res.next())
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
		
	}
	
	
	public Iterator<Client> lister(){
		ResultSet res= null;
		try {
			res=plister.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		List<Client> lesCli = new ArrayList<Client>();
		try {
			while(res.next()){
				lesCli.add(rechercher(res.getInt(1)));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lesCli.iterator();
	}
	
	public Client rechercherLeClientDeLaCommande(int id) {
		ResultSet res = null;
		try {
			prechercherLeClientDeLaCommande.setInt(1, id);
			res = prechercherLeClientDeLaCommande.executeQuery();
		}catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			if(res.next())
				return rechercher(res.getInt(1));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
